var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#witapp',
    data:{
        showList: true,
        title : null,
        dept : {
            parentName:null,
            parentId:0,
            orderNum:0
        }
    },
    methods: {
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/select", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("deptId", vm.dept.parentId);
                if(node){
                    ztree.selectNode(node);
                    vm.dept.parentName = node.name;
                }
            });
        },
        add: function(){
            var roleId = sessionStorage.getItem("roleId") || '';
            if(roleId == null || roleId == ''){
                alert("没有操作权限");
                return;
            }
            vm.dept = {parentName:null,parentId:0,orderNum:0};
            vm.getDept();
            vm.dept.deptId = null;
            this.openDialog();
        },
        update: function () {
            var roleId = sessionStorage.getItem("roleId") || '';
            if(roleId == null || roleId == ''){
                alert("没有操作权限");
                return;
            }
            var _this = this;
            var deptId = getDeptId();
            if(deptId){
                ajaxQuery(baseURL + "sys/dept/info/"+deptId,{},function(data){
                    if(data.code == 0){
                        vm.dept = data.dept;
                        vm.getDept();
                        _this.openDialog(deptId);
                    }else{
                        alert(data.msg);
                    }
                });
            }
        },
        del: function () {
            var deptId = getDeptId();
            if(deptId){
                confirm('确定要删除选中的记录？', function(){
                    ajaxExecute(baseURL + "sys/dept/delete?deptId="+deptId,{},function(data){
                        if(data.code == 0){
                            vm.reload();
                        }else{
                            alert(data.msg);
                        }
                    });
                });
            }
        },
        enable: function () {
            var deptId = getDeptId();
            if(deptId){
                confirm('确定要启用该部门吗?', function(){
                    ajaxExecute(baseURL + "sys/dept/enable?deptId="+deptId,{},function(data){
                        if(data.code == 0){
                            vm.reload();
                        }else{
                            alert(data.msg);
                        }
                    });
                });
            }
        },
        openDialog : function(kid){
            var url = "/sys/dept/save";
            var title = '添加部门';
            if(kid != null && kid.length >0){
                url = '/sys/dept/update?data_id='+kid;
                title = '编辑部门';
            }
            addOrEdit(title,'#div_dept_dialog',['600px','520px'],function(indexLayero,layero){
                var roleId = sessionStorage.getItem("roleId") || '';
                if(roleId == null || roleId == ''){
                    alert("没有操作权限");
                    return;
                }
                var subjectId = sessionStorage.getItem("subjectId");
                if(subjectId == null){
                    alert("你还未添加经营主体!");
                    return;
                }
                ajaxExecute(url,JSON.stringify(vm.dept),function(data){
                    if(data.code == 0){
                        layer.close(indexLayero);
                        dialogResult(data.msg);
                        vm.reload();
                    }else{
                        alert(data.msg);
                    }
                });
            });
        },
        deptTree: function(){
            var roleId = sessionStorage.getItem("roleId") || '';
            if(roleId == null || roleId == ''){
                alert("没有操作权限");
                return;
            }
            layer.open({
                type: 1,
                title: "选择部门",
                area: ['570px', '450px'],
                btnAlign : 'c',
                shade: 0.2,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定','取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    if(node){
                        //选择上级部门
                        vm.dept.parentId = node[0].deptId;
                        vm.dept.parentName = node[0].name;
                    }
                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});

var Dept = {
    id: "deptTable",
    table: null,
    layerIndex: -1
};

function getDeptId (){
    var row = getSelectedRow();
    if(row){
        return row;
    }
}

$(function (){
    $("#jqGrid").jqGrid({
        url: baseURL + "sys/dept/getListData",
        datatype: "json",
        colModel: [
            { label: '主键', hidden:true, name: 'deptId', index: "dept_id", width: 45, key: true},
            { label: '经营主体名称', name: 'subjectName', width: 95},
            { label: '部门名称', name: 'name',index: "name", width: 55},
            { label: '上级部门', name: 'parentName', width: 55},
            { label: '排序号', name: 'orderNum', width: 75},
            { label: '状态', name: 'delFlag',index: "del_flag", width: 22, formatter: function(value, options, row){
                var show = "";
                switch (value){
                    case -1:
                        show = '<span class="label label-danger">禁用</span>';
                        break;
                    case 0:
                        show = '<span class="label label-success">正常</span>';
                        break;
                    default:
                        break;
                }
                return show;
            }}
        ],
        viewrecords: true,
        height: (pageInfo().height-130),
        rowNum: 15,
        rowList : [15,50,99],
        loadtext : '<img src="../../statics/plugins/layer/skin/default/loading-1.gif" style="margin-right:10px;"/>正在加载,请稍候……',
        rownumbers: false,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        loadComplete : function(data){
            if(data.code == 0){
                return data;
            }else if(data.code == 205){
                invalidAccess();
            }else{
                layer.alert(data.msg);
            }
        },
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    thisPage = {
        initDom : function(){}
    }
    thisPage.initDom();
});